package com.wjb.xietong.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class PreferencesService {
	public static final String PREFERENCE_KEY_USER_NAME = "userName";
	public static final String PREFERENCE_KEY_USER_PASSWORD = "userPwd";
	public static final String PREFERENCE_KEY_USER_CLIENT_ID = "clientId";
	public static final String PREFERENCE_KEY_SETTING_TOTAL_SWITCH = "total";
	public static final String PREFERENCE_KEY_SETTING_MODE_SWITCH = "mode";
	public static final String PREFERENCE_KEY_USER_LOGIN = "login";
	
	private Context context;

	public PreferencesService(Context context) {
		this.context = context;
	}

	/**
	 * 保存登录用户信息
	 */
	public void saveLoginUserInfo(String userName, String userPwd, String clientId) {
		SharedPreferences userInfo = context.getSharedPreferences("userInfo",
				Context.MODE_PRIVATE);
		Editor editor = userInfo.edit();
		editor.putString(PREFERENCE_KEY_USER_NAME, userName);
		editor.putString(PREFERENCE_KEY_USER_PASSWORD, userPwd);
		editor.putString(PREFERENCE_KEY_USER_CLIENT_ID, clientId);
		editor.commit();
	}
	
	/**
	 * 清除登录用户信息
	 */
	public void removeLoginUserInfo() {
		SharedPreferences userInfo = context.getSharedPreferences("userInfo",
				Context.MODE_PRIVATE);
		Editor editor = userInfo.edit();
		editor.clear();
		editor.commit();
	}
	
	/**
	 * 保存个推clientId
	 * @param clientId
	 */
	public void saveClientId(String clientId){
		SharedPreferences userInfo = context.getSharedPreferences("userInfo",
				Context.MODE_PRIVATE);
		Editor editor = userInfo.edit();
		editor.putString(PREFERENCE_KEY_USER_CLIENT_ID, clientId);
		editor.commit();
	}

	/**
	 * 根据Key获取各项参数
	 * 
	 * @return String value
	 */
	public String getLoginUserInfoPreference(String key) {
		SharedPreferences userInfo = context.getSharedPreferences("userInfo",
				Context.MODE_PRIVATE);
		return userInfo.getString(key, null);
	}

	/**
	 * 保存参数
	 * 
	 * 推送总设置
	 * 
	 * 夜间免打扰模式设置
	 */
	public void saveSettingMode(boolean totalSwitch, boolean flymode) {
		// 获得SharedPreferences对象
		SharedPreferences preferences = context.getSharedPreferences(
				"systemsetting", Context.MODE_PRIVATE);
		Editor editor = preferences.edit();
		editor.putBoolean(PREFERENCE_KEY_SETTING_TOTAL_SWITCH, totalSwitch);
		editor.putBoolean(PREFERENCE_KEY_SETTING_MODE_SWITCH, flymode);
		editor.commit();
	}

	/**
	 * 根据Key获取各项参数
	 * 
	 * @return boolean value
	 */
	public boolean getSettingPerference(String key) {
		boolean params;
		SharedPreferences preferences = context.getSharedPreferences(
				"systemsetting", Context.MODE_PRIVATE);
		params = preferences.getBoolean(key, false);
		return params;
	}
	
	public void saveUserLogin(String login){
		SharedPreferences userInfo = context.getSharedPreferences("userInfo",
				Context.MODE_PRIVATE);
		Editor editor = userInfo.edit();
		editor.putString(PREFERENCE_KEY_USER_LOGIN, login);
		editor.commit();
	}

	public String getUserLogin(){
		return getLoginUserInfoPreference(PREFERENCE_KEY_USER_LOGIN);
	}
}
